package com.training1.core.web.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.entity.CompanyNameEntity;
import com.training1.core.entity.ContractDetEntity;
import com.training1.core.entity.ContractInfoEntity;
import com.training1.core.entity.FirContractEntity;
import com.training1.core.entity.FirSecContractExeEntity;
import com.training1.core.entity.HelpEntity;
import com.training1.core.modules.service.CompanyService;
import com.training1.core.modules.service.ContractDetService;
import com.training1.core.modules.service.ContractInfoService;
import com.training1.core.modules.service.FirContractService;

@Controller
@RequestMapping("firContract")
public class FirContractController extends BaseController{
	
	@Resource
	private CompanyService companyService;
	@Resource
	private FirContractService firContractService;
	
	
	/**
	 * 进入合同一审页面
	 * @return
	 */
	@RequestMapping(value={"","/"})
	public String go(String id){
		
		return "firContract/firContract";
	}
	/**
	 * 进入一审详情页面
	 * @return
	 */
	@RequestMapping(value="/firContractExe")
	public String goFirContractExe(String id,Model model){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		
		FirSecContractExeEntity firContractExe = firContractService.findContractExeById(param);
		
		model.addAttribute("firContractExe",firContractExe);
		
		return "firContract/firContractExe";
	}
	/**
	 * 查询所有的公司的名称
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/findCompany")
	@ResponseBody
	public List<CompanyNameEntity> findAllCompany(){
		CompanyNameEntity name = new CompanyNameEntity();
		name.setCompanyName("全部");
		name.setId(null);
		List<CompanyNameEntity> companies	= companyService.findAllCompanyName();
		companies.add(0, name);
		
		return companies;
	}
	
	
	/**
	 * 查询所有一审合同
	 * @param title
	 * @param keyWord
	 * @param isValid
	 * @param page
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	@ResponseBody
	public Page<FirContractEntity> list (String cellphone, String userName, String companyName,String recom, Page<FirContractEntity> page){
	
		page.getSearch().put("cellphone", cellphone);
		page.getSearch().put("userName", userName);
		page.getSearch().put("companyName", companyName);
		page.getSearch().put("recom", recom);
		
		firContractService.findAllFirContract(page.getSearch());
		
		return page;
		
	}
	/**
	 * 导出excel
	 * @param resp
	 * @param blackListFlag
	 * @param cellphone
	 * @param memberName
	 * @param branchId
	 * @param userTrust
	 */
	@RequestMapping("export")
	@ResponseBody
	public void export(HttpServletResponse resp, String userName,String companyName,
			String cellPhone,String recom ){
		//设置参数
	Map<String, Object> param = new HashMap<String, Object>();
	param.put("userName", userName);
	param.put("companyName", companyName);
	param.put("cellPhone",cellPhone);
	param.put("recom",recom);
	
	firContractService.export(resp, param);
}
	
	/**
	 * 查询一审合同详情
	 * @param title
	 * @param keyWord
	 * @param isValid
	 * @param page
	 * @return
	 */
	@RequestMapping(value="listExe",method=RequestMethod.GET)
	@ResponseBody
	public FirSecContractExeEntity list (String id){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		
		FirSecContractExeEntity firContractExe = firContractService.findContractExeById(param);
		
		return firContractExe;
		
	}
	/**
	 * 拒绝  /同易
	 * @param id
	 * @return
	 */
	@RequestMapping(value="refuseOrAgree" ,method={RequestMethod.POST})
	@ResponseBody
	public String  refuse ( String status,String id,String adviseAmount,String adviseCyc,String recom,String recomPhone,String firInsReason,String secInsReason ){
		
		String result;
		Map<String, Object> param = new HashMap<String, Object>();
			if (status == "0"){//拒绝
				param.put("id", id);
				param.put("firInsReason", firInsReason);
				param.put("firInsDate", new Date());
				param.put("firInsOperId", "4");
				 result = firContractService.refuse(param);
		}else{
			param.put("id", id);
			param.put("adviseAmount", adviseAmount);
			param.put("adviseCyc", adviseCyc);
			param.put("recom", recom);
			param.put("recomPhone", recomPhone);
			param.put("firInsDate", new Date());
			param.put("firInsOperId", "4");
			
			result = firContractService.agree(param);
		}
		return result;
		
	}
}
